import {
    msg,
    close,
    getQueryStringFromHash,
    nextTick
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json
} from '../../common/config.js';
import QuestionService from '../services/questionService.js';
import SubjectService from '../services/subjectService.js';
import QuestionTypeService from '../services/questionTypeService.js';

window.Module = function(){
    let questionService = new QuestionService();
    let subjectService = new SubjectService();
    let questionTypeService = new QuestionTypeService();
    return new Vue({
        el: '#updatePageController',
        data: {
            entity: {
                answers:[],
                subjectId:0
            },
            questionTypes: [],
            subjects: [],
            sort:0,
            id:0
        },
        methods:{
            init:async function(){
                await this.loadSubject();
                await this.loadQuestionType();
                let id = getQueryStringFromHash('id');
                if(!id)
                    close();
                let res =await questionService.getQuestionsById(id);
                if(json == res.type){
                    var response = res.body;
                    if('Success' === response.result){
                        this.id =id;
                        this.entity = response.data;
                        this.sort= this.entity.answers.length;
                        this.entity.subjectId =this.entity.subjects.id;
                    }
                    else{
                        await msg(response.message);
                    }
                }
            },
            close:function(){
                close();
            },
            loadSubject: async function () {
                let res = await subjectService.getSubject(1, 1000);

                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.subjects = response.data.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            loadQuestionType: async function () {
                let res = await questionTypeService.getQuestionType();

                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.questionTypes = response.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            addAnswer:async function(){
                this.entity.answers.push({
                    sort:this.sort++
                });
                await nextTick();
                $(document)[0].dispatchEvent(new Event('icheck:render'));
            },
            deleteAnswer:async function(index){
                this.entity.answers.splice(index,1);
            },
            changeQuestionType: function(){
                if(this.entity.questionType=='ShortAnswerQuestion')
                    this.entity.answers=[];
            },
            save:async function(){
                if (!validator.checkAll('form')) {
                    layer.msg('请填写正确参数');
                    return;
                }
                let res = await questionService.updateQuestions(this.id, this.entity);
                if (!res.type) {
                    for (var item in res) {
                        msg(res[item][0]);
                        break;
                    }
                }
                else {
                    var response = res.body;
                    if ('Success' == response.result) {
                        this.close();
                    }
                    else {
                        msg(response.message);
                    }
                }
            }
        }
    });
}